﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace uk.ac.soton.ses
{
    public class HDCWatcherQueueItem_FolderCheckWithEvent : HDCWatcherQueueItem_FolderCheckQuick
    {
        //private HDCPath _hdcPath = null;
        //public HDCPath HDCPath { get { return this._hdcPath; } }

        private FileSystemEventArgs _eventArgs = null;

        //Properties
        public FileSystemEventArgs EventArgs { get { return this._eventArgs; } }

        //Constructor
        public HDCWatcherQueueItem_FolderCheckWithEvent(FileSystemEventArgs eventArgs, HDCFileSystemWatcher fileSystemWatcher)
            : base(eventArgs.FullPath, fileSystemWatcher)
        {
            this._eventArgs = eventArgs;
        }

        public override bool IsValid()
        {
            if (!base.IsValid())
            {
                return false;
            }

            //Check for missing file. The logic goes that when it's not an event the file should always exist
            //If it is an event, the file should always exist for all types of event except delete.
            if (base._hdcPath.PathType == PathType.Unknown && this._eventArgs.ChangeType != WatcherChangeTypes.Deleted)
            {
                string message = string.Format("{0} does not exist, ignoring", base._hdcPath.FullPath);
                HDCFileSystemWatcher.Message(message, TraceLevel.Warning);
                return false;
            }

            //If file has been renamed, let's collect some information
            if (this._eventArgs.ChangeType == WatcherChangeTypes.Renamed && !(this._eventArgs is RenamedEventArgs))
            {
                string message = string.Format("{0} was renamed, but a RenamedEventArgs was not received so not processing this event any further as I don't know what to do", base._hdcPath.FullPath);
                HDCFileSystemWatcher.Message(message, TraceLevel.Error);
                return false;
            }

            return true;
        }

        public override void DoWork()
        {
            //For debug purposes, print that we are processing an event
            FileSystemEventArgs eventargs = ((HDCWatcherQueueItem_FolderCheckWithEvent)this).EventArgs;

            string message = string.Format("Processing event \"{0}\"", eventargs.ChangeType);
            HDCFileSystemWatcher.Message(message, TraceLevel.Info);

            base.DoWork();
        }
    }
}
